const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");


const CLIPDATA_TYPE = {
    max:{
        fixedhp: 298.75,
        fixedAtt: 19.45,
        fixdefence: 23.15,
        reaction : 23.31,
        attRate: 5.83, 
        hpRate: 5.83, 
        criticalRate: 3.89,
        criticalStrike: 7.77,
        defRate: 7.29,
        charging: 6.48
    },
    mid:{
        fixedhp: 254,   // 209  239   269   299
        fixdefence: 19.75, // 16   19  21  23
        fixedAtt: 16.75,   //  14   16   18  19
        reaction : 19.75,  // 16  19   21   23
        attRate: 4.957,  // 4.1   4.7   5.3  5.8
        hpRate: 4.957,   // 4.1  4.7  5.3  5.8
        criticalRate: 3.3,   //  2.7  3.1   3.5   3.9
        criticalStrike: 6.6,  //  5.4  6.2  7  7.8
        defRate: 6.2,   //  5.1  5.8  6.6   7.3
        charging: 5.5  //  4.5   5.2   5.8   6.5
    }
}

const CLIPDATA = CLIPDATA_TYPE.mid

const CONFIG = {
    chance: 20,
    your_lv: 81,
    enemy_lv: 90,
    clips_main: {
        criticalStrike: 62.2,
        dmgRate: 46.6,
        attRate: 46.6,
        // reaction: 187,
    },
    clips: ["reaction", "attRate", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["fixedAtt", "reaction", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["fixedAtt", "reaction", "attRate", "criticalRate"]
    ],
    reactionK : 1.5,
    reactionDmgAdd: 0,
    buffs: [  
        // { dmgRate: 50, attRate: 18, dmgZhi:0}, // 追忆
        { reaction: 80, dmgRate: 35, dmgZhi:0}, // 流浪
        { attRate: 20 + 25, fixedAtt:1202 },
        { jk: 15 },
        { dmgRate: 35, jk:20 },
        // { dmgRate: 40 + 16, attRate:20 , reaction:0, jk:40 },
        // { dmgZhi: 4000, dmgRate: 15 }
    ]
}

let charaters = {
    units : {
        name: "甘雨",
        reaction: 0,
        charging: 0,
        defRate: 0,
        defence: 630,
        hp:9797,
        base: 335,
        criticalRate: 5 + 20,
        criticalStrike: 88.4,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}

/**
0命甘雨  乐团 攻冰爆 (以下伤害为暴击期望伤害 , 平均暴击伤害)
   吃队友拐 万班香  0命万(40减抗 40冰伤 仓古16增伤 20攻击)班(宗室20攻 大招固定攻击1203)香(双火25攻)
         阿莫斯(5精)  攻击2381|4197 精通198.5 双爆 77.6/190 
            伤害: 蓄力第二段89983 第二段融化181570   升1命伤害 95851/193412
         阿莫斯(1精)  攻击2381|4197 精通198.5 双爆 77.6/190 
            伤害: 蓄力第二段76285 第二段融化153930   升1命伤害 81260/163969
         天空(1精)  攻击2022|3881 精通258 双爆 80/230 
            伤害: 蓄力第二段66834 第二段融化143585   升1命伤害 71193/152949
         破魔(5精)  攻击2091|3807 精通198.5 双爆 77.6/190 
            伤害: 蓄力第二段68251 第二段融化137719   升1命伤害 72702/146700

    吃队友拐 钟香班   钟离(20减抗 攀岩35增伤)班(宗室20攻 大招固定攻击1203)香(双火25攻)
         阿莫斯(5精)  攻击2381|吃拐后4008 精通198.5 双爆 77.6/190 
            伤害: 蓄力第二段73642 第二段融化148597  升1命伤害 78902/159211
         阿莫斯(1精)  攻击2381|吃拐后4008 精通198.5 双爆 77.6/190 
            伤害: 蓄力第二段61697 第二段融化124495  升1命伤害 66105/133388
         天空(1精)  攻击2023|吃拐后3680 精通258 双爆 80/230 
            伤害: 蓄力第二段52737 第二段融化113299  升1命伤害 56504/121392
         破魔(5精)  攻击2091|吃拐后3649 精通198.5 双爆 77.6/190 
            伤害: 蓄力第二段55341 第二段融化111669  升1命伤害 59294/119645
 */

calc(calc_clips_initial());

function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        if(weapon.weapontype == "bow") {
            charactersDmg.push(getDMG([charaters.units,weapon,{fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
                count++;
                data = objValueAdd([data, ...CONFIG.buffs]);
                data.hp = data.hp*(1 + (data.hpRate||0)/100) + 4780;
                if(weapon.skill) { weapon.skill(data); }
    
                let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:CONFIG.reactionDmgAdd }
                // 2.304  3.9168
                let A1Dmg = getDmg({ data, rate: 2.304}).qw * reduceResistanceK(10- (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let A2Dmg = getDmg({ data, rate: 3.9168}).qw * reduceResistanceK(10- (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let A1DmgRea = getDmg({ data, rate: 2.304, rea}).qw * reduceResistanceK(10- (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let A2DmgRea = getDmg({ data, rate: 3.9168, rea}).qw * reduceResistanceK(10- (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
    
                if(data.phyRate) { // 物理特效伤害
                    data.phyDmg = getDmg({data:{...data, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
                }
    
                let point = (A1DmgRea + A2DmgRea) * 3 + (data.phyDmg||0) * 0;
                let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
                return {point, data, second, attack,msg:{A1Dmg, A2Dmg,A1DmgRea, A2DmgRea}}
            }))
        }
    })
    // 攻击力 2551  精通 197.5 双爆91/177 增伤148.6 10级普攻两段倍率6.2208
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point);
    });
    console.log(charactersDmg[1]);
    console.log(charactersDmg[5]);
    console.log(charactersDmg[10]);
    console.log(charactersDmg[11]);
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                });
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return getRet(panel, second)
    })
    _ret.sort((b,a)=> a.point-b.point)
    return _ret[0];
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2];
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
